#include "dvLineChart.h"
#include <qwt_plot_curve.h>
#include <qwt_interval_symbol.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include <qwt_plot_canvas.h>
#include <qwt_scale_div.h>
#include <qwt_scale_draw.h>
#include <qwt_curve_fitter.h>
#include <QDate>

class YearScaleDraw: public QwtScaleDraw
{
public:
    YearScaleDraw(const QStringList& listDates)
    {
        m_dates = listDates;

        setTickLength( QwtScaleDiv::MajorTick, 8);
        setTickLength( QwtScaleDiv::MinorTick, 0);
        setTickLength( QwtScaleDiv::MediumTick, 0);

        setLabelRotation(-60.0);
        setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);

        setSpacing(5);
    }

    virtual QwtText label(double value) const
    {
        QwtText text("");
        QFont font("Times New Roman", 11);
        font.setBold(true);
        text.setFont(font);
        int index = int(value);

        if (index >= 0 && index < m_dates.count())
        {
            text.setText(m_dates.at(index));
        }
        else
        {
            text = QwtScaleDraw::label(value);
        }

        return text;
    }

private:
    QStringList m_dates;
};


dvLineChart::dvLineChart(QWidget *parent) :
    QwtPlot(parent)
{
    m_fittedSpline = false;
    m_showLegend = true;
    showLegend(m_showLegend);
    canvas()->setPalette(Qt::white);
    canvas()->setBorderRadius(5);
    canvas()->setCursor(Qt::ArrowCursor);

    connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)),
        SLOT(showItem(QwtPlotItem *, bool)));

    setAutoReplot(true);
}

dvLineChart::~dvLineChart()
{
    clear();
    if (m_pLegend) delete m_pLegend;
}

void dvLineChart::clear()
{
    setTitle("");
    setAxisScaleDiv(QwtPlot::xBottom,QwtScaleDiv());
    setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw);
    setAxisScale(QwtPlot::xBottom,0,1000);
    setAxisScale(QwtPlot::yLeft,0,1000);
    setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw);
    foreach(QwtPlotCurve* curve, m_curves.values())
    {
        if (curve)
        {
            curve->detach();
            delete curve;
        }
    }
    m_curves.clear();
    if (m_showLegend)
    {
        showLegend(false);
        m_showLegend = true;
    }
    else
        showLegend(false);
}

void dvLineChart::setDates(const QStringList &list)
{
    setAxisScaleDraw(QwtPlot::xBottom, new YearScaleDraw(list));
    m_dates = list;
}

void dvLineChart::insertCurve(const QString &title, int regionId,
                              const QVector<QPointF> &points, const QColor &color)
{
    QwtPlotCurve *pcurve;
    pcurve = new QwtPlotCurve(title);
    pcurve->setRenderHint(QwtPlotItem::RenderAntialiased);
    //pcurve->setStyle(QwtPlotCurve::NoCurve);
    pcurve->setLegendAttribute(QwtPlotCurve::LegendShowSymbol);

    QwtSymbol *symbol = new QwtSymbol(QwtSymbol::Ellipse);
    symbol->setSize(4);
    symbol->setPen(QPen(color));
    pcurve->setPen(QPen(color));
    pcurve->setSymbol(symbol);
    pcurve->setSamples(points);

    pcurve->setStyle(QwtPlotCurve::Lines);
    pcurve->setCurveAttribute(QwtPlotCurve::Fitted, m_fittedSpline);

    QwtSplineCurveFitter *fitter = new QwtSplineCurveFitter();
    fitter->setFitMode(QwtSplineCurveFitter::ParametricSpline);
    fitter->setSplineSize(100);
    pcurve->setCurveFitter(fitter);

    pcurve->attach(this);
    m_curves.insert(regionId, pcurve);
}

void dvLineChart::showLegend(bool show)
{
    m_showLegend = show;
    if (show)
    {
        m_pLegend = new QwtLegend();
        //m_pLegend->setFrameStyle(QFrame::Box|QFrame::Sunken);
        m_pLegend->setItemMode(QwtLegend::CheckableItem);
        insertLegend(m_pLegend, QwtPlot::RightLegend);
        setCheckedItemsLegend();
    }
    else
    {
        insertLegend(NULL, QwtPlot::RightLegend);
        m_pLegend = NULL;
    }
}

void dvLineChart::showCurve(int region, bool show)
{
    QwtPlotCurve* curve = m_curves.value(region, NULL);
    if (curve)
    {
        if (!show) curve->detach();
        else curve->attach(this);
    }
    showLegend(m_showLegend);
    replot();
}

void dvLineChart::setHiddenAllCurve(bool hidden)
{
    foreach(QwtPlotCurve* curve, m_curves)
    {
        if (curve)
        {
            if (hidden) curve->detach();
            else curve->attach(this);
        }
    }
    if (!hidden)
        showLegend(m_showLegend);
    else if (m_showLegend)
    {
        showLegend(false);
        m_showLegend = true;
    }
    else
        showLegend(false);
    replot();
}

void dvLineChart::setCheckedItemsLegend()
{
    foreach(QwtPlotCurve* curve, m_curves.values())
    {
        if (curve)
        {
            if (curve->pen().width() == 3)
            {
                QwtLegendItem *item = qobject_cast<QwtLegendItem*>(m_pLegend->find(curve));
                if (item)
                {
                    item->setItemMode(QwtLegend::CheckableItem);
                    item->setChecked(true);
                }
            }
        }
    }
}

void dvLineChart::setSplineCurves(bool spline)
{
    m_fittedSpline = spline;
    foreach(QwtPlotCurve* curve, m_curves)
    {
        if (curve)
        {
            curve->setCurveAttribute(QwtPlotCurve::Fitted, m_fittedSpline);
        }
    }
    replot();
}

void dvLineChart::showItem(QwtPlotItem *item, bool on)
{
    QwtPlotCurve* curve = dynamic_cast<QwtPlotCurve*>(item);
    if (curve)
    {
        QPen pen = curve->pen();
        if (on)
        {
            pen.setWidth(3);
            curve->setPen(pen);
        }
        else
        {
            pen.setWidth(1);
            curve->setPen(pen);
        }
    }
}









































